package com.jumi.microservice.controller;

import com.alibaba.fastjson.JSON;
import com.jumi.microservice.constant.PayChannelEnum;
import com.jumi.microservice.dto.PayNotifyRequest;
import com.jumi.microservice.dto.PayNotifyResponse;
import com.jumi.microservice.service.IPayCoreService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

/**
 * @author scw
 * @date 2020/10/31
 */
@RestController
@RequestMapping("/pay/notify")
public class PayNotifyController {
    @DubboReference
    IPayCoreService iPayCoreService;
    private Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 支付宝
     * 支付结果异步回调
     *
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/ali")
    public String aliPayNotify(HttpServletRequest httpServletRequest) {
//        //新版本
//        Map<String, String> alipayRequestParams = new HashMap<>();
//        Enumeration<?> temp = httpServletRequest.getParameterNames();
//        while (temp.hasMoreElements()) {
//            String en = (String) temp.nextElement();
//            String value = httpServletRequest.getParameter(en);
//            alipayRequestParams.put(en, value);
//        }
//        PayNotifyRequest request = new PayNotifyRequest();
//        request.setPayChannel(PayChannelEnum.ALI_PAY.name());
//        request.setResultMap(alipayRequestParams);
//        PayNotifyResponse payNotifyResponse = iPayCoreService.paymentNotify(request);
//        return payNotifyResponse.getBody();
        Map<String, String[]> alipayRequestParams = httpServletRequest.getParameterMap();
        log.info("支付宝回调参数：[{}]", JSON.toJSONString(alipayRequestParams));
        PayNotifyRequest request = new PayNotifyRequest();
        request.setResultMap(alipayRequestParams);
        request.setPayChannel(PayChannelEnum.ALI_PAY.name());
        PayNotifyResponse payNotifyResponse = iPayCoreService.paymentNotify(request);
        return payNotifyResponse.getBody();
    }

    /**
     * 支付宝H5
     * 支付结果异步回调
     *
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/aliH5")
    public String aliPayH5Notify(HttpServletRequest httpServletRequest) {
        Map<String, String[]> alipayRequestParams = httpServletRequest.getParameterMap();
        PayNotifyRequest request = new PayNotifyRequest();
        request.setResultMap(alipayRequestParams);
        request.setPayChannel(PayChannelEnum.ALI_PAY_H5.name());
        request.setResultMap(alipayRequestParams);
        PayNotifyResponse payNotifyResponse = iPayCoreService.paymentNotify(request);
        return payNotifyResponse.getBody();
    }

    /**
     * 微信支付异步回调
     *
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/wx")
    public String wxPayNotify(HttpServletRequest httpServletRequest) {
        StringBuffer stringBuffer = new StringBuffer();
        BufferedReader bufferedReader = null;
        InputStream inputStream = null;
        try {
            inputStream = httpServletRequest.getInputStream();
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String a;
            while ((a = bufferedReader.readLine()) != null) {
                stringBuffer.append(a);
            }
            log.info("微信支付回调通知:{}", stringBuffer.toString());
        } catch (IOException e) {
            log.error("解析微信回调数据错误{}", e.getMessage());
            e.printStackTrace();
        } finally {
            if (null == bufferedReader) {
                try {
                    inputStream.close();
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("关闭流错误{}", e.getMessage());
                }
            }
        }
        PayNotifyRequest payNotifyRequest = new PayNotifyRequest();
        payNotifyRequest.setPayChannel(PayChannelEnum.WX_PAY.name());
        payNotifyRequest.setXml(stringBuffer.toString());
        PayNotifyResponse payNotifyResponse = iPayCoreService.paymentNotify(payNotifyRequest);
        return payNotifyResponse.getBody();
    }

    /**
     * 微信支付H5异步回调
     *
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/wxH5")
    public String wxH5PayNotify(HttpServletRequest httpServletRequest) {
        StringBuffer stringBuffer = new StringBuffer();
        BufferedReader bufferedReader = null;
        InputStream inputStream = null;
        try {
            inputStream = httpServletRequest.getInputStream();
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String a;
            while ((a = bufferedReader.readLine()) != null) {
                stringBuffer.append(a);
            }
            log.info("微信支付回调通知:{}", stringBuffer.toString());
        } catch (IOException e) {
            log.error("解析微信回调数据错误{}", e.getMessage());
            e.printStackTrace();
        } finally {
            if (null == bufferedReader) {
                try {
                    inputStream.close();
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("关闭流错误{}", e.getMessage());
                }
            }
        }
        PayNotifyRequest payNotifyRequest = new PayNotifyRequest();
        payNotifyRequest.setPayChannel(PayChannelEnum.WX_PAY_H5.name());
        payNotifyRequest.setXml(stringBuffer.toString());
        PayNotifyResponse payNotifyResponse = iPayCoreService.paymentNotify(payNotifyRequest);
        return payNotifyResponse.getBody();
    }

    /**
     * 微信支付小程序异步回调
     *
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/wxlets")
    public String wxLetsPayNotify(HttpServletRequest httpServletRequest) {
        StringBuffer stringBuffer = new StringBuffer();
        BufferedReader bufferedReader = null;
        InputStream inputStream = null;
        try {
            inputStream = httpServletRequest.getInputStream();
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String a;
            while ((a = bufferedReader.readLine()) != null) {
                stringBuffer.append(a);
            }
            log.info("微信支付回调通知:{}", stringBuffer.toString());
        } catch (IOException e) {
            log.error("解析微信回调数据错误{}", e.getMessage());
            e.printStackTrace();
        } finally {
            if (null == bufferedReader) {
                try {
                    inputStream.close();
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("关闭流错误{}", e.getMessage());
                }
            }
        }
        PayNotifyRequest payNotifyRequest = new PayNotifyRequest();
        payNotifyRequest.setPayChannel(PayChannelEnum.WX_PAY_LETS.name());
        payNotifyRequest.setXml(stringBuffer.toString());
        PayNotifyResponse payNotifyResponse = iPayCoreService.paymentNotify(payNotifyRequest);
        return payNotifyResponse.getBody();
    }
}
